/* === This file is part of Calamares - <https://calamares.io> ===
 *
 *   SPDX-FileCopyrightText: 2014 Teo Mrnjavac <teo@kde.org>
 *   SPDX-License-Identifier: GPL-3.0-or-later
 *
 *   Calamares is Free Software: see the License-Identifier above.
 *
 */

#include "ProgressTreeView.h"

#include "ProgressTreeDelegate.h"

#include "Branding.h"
#include "ViewManager.h"
#include "utils/Logger.h"

ProgressTreeView::ProgressTreeView( QWidget* parent )
    : QListView( parent )
{
    this->setObjectName( "sidebarMenuApp" );
    setFrameShape( QFrame::NoFrame );
    setContentsMargins( 0, 0, 0, 0 );

    setFlow(QListView::LeftToRight);//
    setSelectionMode( QAbstractItemView::NoSelection );
    setDragDropMode( QAbstractItemView::NoDragDrop );
    setAcceptDrops( false );
//    setVisible( false);

    setItemDelegate( new ProgressTreeDelegate( this ) );

    QPalette plt = palette();
    plt.setColor( QPalette::Base,
                  Calamares::Branding::instance()->styleString( Calamares::Branding::SidebarBackground ) );
    setPalette( plt );
//    connect(parent, SIGNAL(currentStepChanged(int)), this, SLOT(updateVisibility(int)));
    connect( Calamares::ViewManager::instance(),
         &Calamares::ViewManager::currentStepChanged,
         this,
         &ProgressTreeView::updateVisibility,
         Qt::UniqueConnection );
//    connect(viewManager, &ViewManager::currentStepChanged, this, &ProgressTreeView::updateVisibility);
}


ProgressTreeView::~ProgressTreeView() {}



void
ProgressTreeView::setModel( QAbstractItemModel* model )
{
    if ( ProgressTreeView::model() )
    {
        return;
    }

    QListView::setModel( model );

    connect( Calamares::ViewManager::instance(),
             &Calamares::ViewManager::currentStepChanged,
             this,
             &ProgressTreeView::update,
             Qt::UniqueConnection );
}

void
ProgressTreeView::update()
{
    cDebug() << "update ---------------------";
    viewport()->update();
}

void
ProgressTreeView::updateVisibility(int currentStep)
{
    cDebug() << "int currentStep ------------------ " << currentStep;
    // 在第一个步骤隐藏，其他步骤显示
    this->setVisible(currentStep > 0);
}
